package com.SmartCare.controller.manager;

import com.SmartCare.common.Result;
import com.SmartCare.pojo.homeInfo.CareHomeDetails;
import com.SmartCare.service.*;
import com.SmartCare.service.homeInfo.DetailsService;
import com.SmartCare.service.homeInfo.AppreciateService;
import com.SmartCare.service.homeInfo.ContactsService;
import com.SmartCare.service.homeInfo.PriceDetailsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 养老院信息详情表 前端控制器
 */
@RestController
@RequestMapping("/details")
public class DetailsController {
    @Resource
    DetailsService detailsService;
    @Resource
    AppreciateService appreciateService;
    @Resource
    PriceDetailsService priceDetailsService;
    @Resource
    ContactsService contactsService;

    @Resource
    ImageService imageService;

    @Value("${files.upload.path}/careHomeImg/")
    private String fileUploadPath;


    //根据名称搜索养老院信息
    @GetMapping("/search")
    @Operation(summary = "搜索养老院信息", description = "根据名称搜索养老院")
    public Result search(@Parameter(description = "具体查询的页数", required = true) @RequestParam Integer pageNum,
                         @Parameter(description = "每页数据的条数", required = true) @RequestParam Integer pageSize,
                         @Parameter(description = "搜索关键词", required = true) @RequestParam String keyword) {
        return detailsService.search(pageNum, pageSize, keyword);
    }

    //根据id获取养老院评价信息
    @GetMapping("/get-appreciate/{id}")
    @Operation(summary = "获取评价信息", description = "根据id获取养老院评价信息")
    public Result getAppreciate(@Parameter(description = "具体查询的页数", required = true) @PathVariable Long id) {
        return appreciateService.getAppreciate(id);
    }

    //根据id获取养老院价格信息
    @GetMapping("/get-price/{id}")
    @Operation(summary = "获取价格信息", description = "根据id获取养老院价格信息")
    public Result getPrice(@Parameter(description = "具体查询的页数", required = true) @PathVariable Long id) {
        return priceDetailsService.getPrice(id);
    }

    //根据id获取养老院联系方式
    @GetMapping("/get-contact/{id}")
    @Operation(summary = "获取联系方式", description = "根据id获取养老院联系方式")
    public Result getContact(@Parameter(description = "具体查询的页数", required = true) @PathVariable Long id) {
        return contactsService.getContact(id);
    }

    //添加食物图片
    @PostMapping("/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "details/download");
    }

    //下载食物图片
    @GetMapping("/download/{fileUuid}")
    @Operation(summary = "下载图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }

    //添加养老院信息
    @PostMapping("/add")
    public Result add(@RequestBody CareHomeDetails careHomeDetails) {
        return detailsService.add(careHomeDetails);
    }

    //修改养老院信息
    @PutMapping("/edit")
    public Result edit(@RequestBody CareHomeDetails careHomeDetails) {
        return detailsService.edit(careHomeDetails);
    }

    //根据养老院名称获取养老院信息
    @GetMapping("/query-by-name")
    public Result getDetailsByName(@RequestParam String name){
        return detailsService.getDetailsByName(name);
    }


}

